//
//  GYLoginViewController.m
//  LockManagement
//
//  Created by apple on 2018/7/25.
//  Copyright © 2018年 yunjiazhineng. All rights reserved.
//

#import "GYLoginViewController.h"
#import "GYCompany.h"
#import "GYCompayListView.h"
#import "GYNetWorkTools.h"
#import "GYUserInfo.h"


@interface GYLoginViewController ()
@property (weak, nonatomic) IBOutlet UIView *loginView;

@property (weak, nonatomic) IBOutlet UILabel *companyNameLable;
@property (weak, nonatomic) IBOutlet UITextField *ipTF;
@property (weak, nonatomic) IBOutlet UITextField *portTF;
@property (weak, nonatomic) IBOutlet UITextField *userNameTF;
@property (weak, nonatomic) IBOutlet UITextField *pwdTF;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;
@property (weak, nonatomic) IBOutlet UIButton *remeberPwdBtn;
@property (weak, nonatomic) IBOutlet UIImageView *remeberImageView;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *logoTopMarginCons;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *loginViewTopMarginCons;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *TFHeightCons;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *loginViewBottomMarginCons;
@property (weak, nonatomic) IBOutlet UILabel *logoLable;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *loginBtnTopMarginCons;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *remberBtnTopMarginCons;
@property (weak, nonatomic) IBOutlet UIView *bgVIew;


/** 当前选择的公司 */
@property (nonatomic, strong) GYCompany *currentModel;

/** 选择公司列表的视图 */
@property (nonatomic, strong) GYCompayListView *companyListView;

/** 登录界面的背景图 */
@property (nonatomic, strong) UIImageView *bgImageView;
@end

@implementation GYLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
     self.bgImageView.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight-self.bgVIew.height);
}

//初始化
- (void)setupUI {
    //设置属
    self.logoTopMarginCons.constant = heightScale(80, iphone6_H);
    self.loginViewTopMarginCons.constant = heightScale(30, iphone6_H);
    self.TFHeightCons.constant = heightScale(48, iphone6_H);
    self.loginViewBottomMarginCons.constant = heightScale(80, iphone6_H);
    self.logoLable.font = [UIFont systemFontOfSize:heightScale(70, iphone6_H)];
    self.loginBtnTopMarginCons.constant = heightScale(27, iphone6_H);
    self.remberBtnTopMarginCons.constant = heightScale(20, iphone6_H);
    
    
    self.loginBtn.layer.cornerRadius = 23;
    self.loginView.layer.cornerRadius = 5;
    self.loginView.layer.shadowRadius = 5;
    self.loginView.layer.shadowColor = BackGroundColor.CGColor;
    self.loginView.layer.shadowOffset = CGSizeMake(0, 0);
    self.loginView.layer.shadowOpacity = 1.0;
    self.companyNameLable.layer.cornerRadius = 5;
    self.companyNameLable.layer.masksToBounds = YES;
    
    //添加背景图片
    UIImageView *bgImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"login_bg"]];
    [self.view insertSubview:bgImageView atIndex:0];
    self.bgImageView = bgImageView;
    
    
    //取得上次保存的账号密码
    self.userNameTF.text = GETOBJ(@"userName")
    self.pwdTF.text = GETOBJ(@"passWord")
    self.ipTF.text = GETOBJ(@"IP")
    self.portTF.text = GETOBJ(@"port")
    self.remeberPwdBtn.selected = GETOBJ(@"remeberPwd");
    if (self.remeberPwdBtn.selected) {
        self.remeberImageView.image = [UIImage imageNamed:@"gouxuan"];
    }else {
        self.remeberImageView.image = [UIImage imageNamed:@"gouxuan1"];
    }
    NSString *companyName = GETOBJ(@"companyName")
    NSString *companyID = GETOBJ(@"companyID")
    if (companyName && companyID) {
        self.currentModel = [[GYCompany alloc] initWith:companyName companyID:companyID];
        self.companyNameLable.text = companyName;
    }else {
        self.companyNameLable.text = @"请选择公司";
    }
}


- (IBAction)selectCompanyBtnClick:(UIButton *)sender {
    NSLog(@"选择公司名称");
    GYCompayListView *companyView = [GYCompayListView shareView];
    companyView.frame = [UIScreen mainScreen].bounds;
    kWeakSelf(self);
    companyView.companySelectCallBack = ^(GYCompany *model) {
        weakself.companyNameLable.text = model.companyName;
        weakself.currentModel = model;
        
        //删除选择视图
        [weakself.companyListView removeFromSuperview];
    };
    [self.view addSubview:companyView];
    self.companyListView = companyView;
}

/**
 查看输入密码

 @param sender <#sender description#>
 */
- (IBAction)checkPwdBtnClick:(UIButton *)sender {
    self.pwdTF.secureTextEntry = sender.selected;
    sender.selected = !sender.selected;
}


- (IBAction)loginBtnClick:(UIButton *)sender {
    NSLog(@"登陆按钮点击事件...");
    if (!self.currentModel) {
        [SVProgressHUD showErrorWithStatus:@"请选择公司!"];
        return;
    }
    
    if (self.ipTF.text.length == 0 || [self.ipTF.text isEqualToString:@""]) {
        [SVProgressHUD showErrorWithStatus:@"请填写正确IP地址"];
        return;
    }
    
    if (self.portTF.text.length == 0 || [self.portTF.text isEqualToString:@""]) {
        [SVProgressHUD showErrorWithStatus:@"请填写正确端口地址"];
        return;
    }
    
    if (self.userNameTF.text.length == 0 || [self.userNameTF.text isEqualToString:@""]) {
        [SVProgressHUD showErrorWithStatus:@"用户名不能为空"];
        return;
    }
    
    if (self.pwdTF.text.length == 0 || [self.pwdTF.text isEqualToString:@""]) {
        [SVProgressHUD showErrorWithStatus:@"密码不能为空!"];
        return;
    }
    
    //执行登陆请求120.79.103.234 [NSString stringWithFormat:@"http://%@:%@/api/login",self.ipTF.text,self.portTF.text]
    
    [SVProgressHUD showWithStatus:@"正在登陆..."];
    [GYNetWorkTools requestUserLoginWithURL:[NSString stringWithFormat:@"http://%@:%@/api/login",self.ipTF.text,self.portTF.text] companyID:self.currentModel.companyID userName:self.userNameTF.text password:self.pwdTF.text success:^(NSDictionary *obj) {
        //NSLog(@"login obj======%@",obj);
        int state = [[obj objectForKey:@"state"] intValue];
        if (state == 0) {
            //1.判断有没有选择记住密码按钮
            if (self.remeberPwdBtn.selected) {
                SETOBJ(self.userNameTF.text, @"userName")
                SETOBJ(self.pwdTF.text, @"passWord")
                SETOBJ(self.ipTF.text, @"IP")
                SETOBJ(self.portTF.text, @"port")
                SETOBJ([NSNumber numberWithBool:self.remeberPwdBtn.selected], @"remeberPwd")
                SETOBJ(self.currentModel.companyName, @"companyName")
                SETOBJ(self.currentModel.companyID, @"companyID");
            }
            
            //2.保存用户信息
            NSDictionary *data = [obj objectForKey:@"data"];
            GYUserInfo *userInfo = [[GYUserInfo alloc] initWithDic:data];
            [userInfo saveUserInfo];
            
            //3.发送一个通知进入主界面
            [[NSNotificationCenter defaultCenter] postNotificationName:userLoginSuccess object:nil];
        }
        [SVProgressHUD dismiss];
    } error:^(NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"登陆失败,请重新登陆!"];
        NSLog(@"userLogin    error=========%@",error);
    }];
}


/**
 记住密码按钮

 @param sender <#sender description#>
 */
- (IBAction)remeberPwdBtnClick:(UIButton *)sender {
    sender.selected = !sender.selected;
    if (sender.selected) {
        self.remeberImageView.image = [UIImage imageNamed:@"gouxuan"];
    }else {
        self.remeberImageView.image = [UIImage imageNamed:@"gouxuan1"];
        
        //清空保存的账号密码
        REMOVEOBJ(@"userName")
        REMOVEOBJ(@"passWord")
        REMOVEOBJ(@"IP")
        REMOVEOBJ(@"port")
        REMOVEOBJ(@"remeberPwd");
        REMOVEOBJ(@"companyName")
        REMOVEOBJ(@"companyID")
    }
}

//J监听键盘的返回键
- (IBAction)endEdit:(UITextField *)sender {
    [sender resignFirstResponder];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

@end
